package cn.hnc.checkapi.controller;

import cn.hnc.checkapi.bean.Motor;
import cn.hnc.checkapi.bean.NcVer;
import cn.hnc.checkapi.bean.Product;
import cn.hnc.checkapi.bean.ProductSel;
import cn.hnc.checkapi.bean.Servo;
import cn.hnc.checkapi.bean.User;
import cn.hnc.checkapi.bean.datatmp.DataBean;
import cn.hnc.checkapi.bean.datatmp.TransferProdcutAndDatabean;
import cn.hnc.checkapi.bean.response.ProductResponseEntity;
import cn.hnc.checkapi.bean.response.ResponseEntiry;
import cn.hnc.checkapi.bean.response.SimpleResponseEntity;
import cn.hnc.checkapi.logger.WaLogger;
import cn.hnc.checkapi.repository.MotorRepository;
import cn.hnc.checkapi.repository.NcVerRepository;
import cn.hnc.checkapi.repository.ProductRepository;
import cn.hnc.checkapi.repository.ServoRepository;
import cn.hnc.checkapi.service.MotorService;
import cn.hnc.checkapi.service.NodeService;
import cn.hnc.checkapi.service.ProductService;
import cn.hnc.checkapi.service.UserService;
import cn.hnc.checkapi.utils.IpUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ProductController {
   @Autowired
   private HttpServletRequest request;
   @Autowired
   private ProductService productService;
   @Autowired
   private ProductRepository productRepository;
   @Autowired
   private MotorService motorService;
   @Autowired
   private MotorRepository motorRepository;
   @Autowired
   private NcVerRepository ncVerRepository;
   @Autowired
   private ServoRepository servoRepository;
   @Autowired
   private UserService userService;
   @Autowired
   private NodeService nodeService;

   @GetMapping({"/checkapi/needToAdd"})
   @ResponseBody
   public ResponseEntiry needToAdd(Long startId, Long endId) {
      ResponseEntiry productResponseEntiry = new ResponseEntiry();
      productResponseEntiry.setData(this.productService.findNeedAdd(startId, endId));
      Map main = this.nodeService.sendListRequest(this.productService.findNeedAdd(startId, endId), "/identityv2/data/batchCreate");
      System.out.println(main);
      return productResponseEntiry;
   }

   @PostMapping({"/checkapi/addProduct"})
   @ResponseBody
   public ResponseEntiry addProduct(@RequestBody String json, HttpServletRequest request) {
      String ip = IpUtil.getIpAddr(request);
      ResponseEntiry productResponseEntiry = new ResponseEntiry();

      try {
         ObjectMapper mapper = new ObjectMapper();
         DataBean dataBean = (DataBean)mapper.readValue(json, new TypeReference() {
         });
         Product product = TransferProdcutAndDatabean.dataBeanToProduct(dataBean);
         product.setId((Long)null);
         User user = this.userService.findFirstByUsername(dataBean.getLoginName());
         if (user == null) {
            throw new Exception("用户不存在");
         }

         Product product1 = this.productService.save(product, user, ip);
         List products = new ArrayList();
         List productList = new ArrayList();
         products.add(product1);
         Iterator var12 = products.iterator();

         while(var12.hasNext()) {
            Product pro = (Product)var12.next();
            Long id = pro.getId();
            NcVer ncVer = this.ncVerRepository.findFirstByProductId(id);
            List se = this.servoRepository.findByProductId(id);
            List servos = new ArrayList();
            List mServoVerList = new ArrayList();
            Iterator var19 = se.iterator();

            while(var19.hasNext()) {
               Servo servo = (Servo)var19.next();
               if (!"轴S".equals(servo.getAxisName())) {
                  servos.add(servo);
               } else {
                  mServoVerList.add(servo);
               }
            }

            List mo = this.motorRepository.findByProductId(id);
            List motors = new ArrayList();
            List mElecDataList = new ArrayList();
            Iterator var22 = mo.iterator();

            while(var22.hasNext()) {
               Motor motor = (Motor)var22.next();
               if (!"轴S".equals(motor.getAxisName())) {
                  motors.add(motor);
               } else {
                  mElecDataList.add(motor);
               }
            }

            pro.setNcVer(ncVer);
            pro.setServos(servos);
            pro.setMotors(motors);
            pro.setMServoVerList(mServoVerList);
            pro.setMElecDataList(mElecDataList);
            productList.add(pro);
         }

         List dataBeans = TransferProdcutAndDatabean.productsToDataBeans(productList);
         (new Thread(() -> {
            System.out.println("同步线程启动！");

            try {
               Map map = this.nodeService.dealData(product1);
               Map main = this.nodeService.sendRequest((Map)map.get("main"), "/identityv2/data");
               Map var4 = this.nodeService.sendRequest((Map)map.get("info"), "/identityv2/data");
            } catch (Exception var5) {
               Exception e = var5;
               e.printStackTrace();
            }

         })).start();
         productResponseEntiry.setMsgCode(0);
         productResponseEntiry.setMsgDesc("录入成功");
         productResponseEntiry.setData(dataBeans.get(0));
      } catch (Exception var24) {
         Exception e = var24;
         productResponseEntiry.setMsgCode(1);
         productResponseEntiry.setMsgDesc(e.toString());
         WaLogger.logger.warn(e.toString());
      }

      return productResponseEntiry;
   }

   @ApiOperation(
      value = "导出电机数据",
      notes = "根据elecModel导出电机各属性值"
   )
   @PostMapping({"/checkapi/sleElecData"})
   @ResponseBody
   public ResponseEntiry exportElecAtrributes(@RequestBody String json) {
      ResponseEntiry productResponseEntiry = new ResponseEntiry();

      try {
         ObjectMapper mapper = new ObjectMapper();
         Motor motor = (Motor)mapper.readValue(json, new TypeReference() {
         });
         List atrributeByElecModel = this.motorService.findAtrributeByElecModel(motor.getElecModel());
         if (atrributeByElecModel.size() == 0) {
            throw new Exception("no data!");
         }

         productResponseEntiry.setMsgCode(0);
         productResponseEntiry.setMsgDesc("查询成功");
         productResponseEntiry.setData(atrributeByElecModel);
      } catch (Exception var6) {
         Exception e = var6;
         WaLogger.logger.warn(e.toString());
         productResponseEntiry.setMsgCode(1);
         productResponseEntiry.setMsgDesc(e.toString());
      }

      return productResponseEntiry;
   }

   @PostMapping({"/checkapi/selProduct"})
   @ResponseBody
   public ProductResponseEntity selProduct(@RequestBody String json) {
      ProductResponseEntity productResponseEntity = new ProductResponseEntity();

      try {
         ObjectMapper mapper = new ObjectMapper();
         ProductSel productSel = (ProductSel)mapper.readValue(json, new TypeReference() {
         });
         Page productPage = this.productService.selProduct(productSel.getBeginTime(), productSel.getEndTime(), productSel.getCustomerName(), productSel.getContractCode(), productSel.getMacSN(), productSel.getIpcCode(), productSel.getMacNO(), productSel.getQueryFlag(), productSel.getServoId(), productSel.getServoModel(), productSel.getElecId(), productSel.getElecModel(), productSel.getPage());
         List productList = new ArrayList();
         List products = productPage.getContent();
         Iterator var8 = products.iterator();

         while(var8.hasNext()) {
            Product product = (Product)var8.next();
            Long id = product.getId();
            NcVer ncVer = this.ncVerRepository.findFirstByProductId(id);
            List se = this.servoRepository.findByProductId(id);
            List servos = new ArrayList();
            List mServoVerList = new ArrayList();
            Iterator var15 = se.iterator();

            while(var15.hasNext()) {
               Servo servo = (Servo)var15.next();
               if (!"轴S".equals(servo.getAxisName())) {
                  servos.add(servo);
               } else {
                  mServoVerList.add(servo);
               }
            }

            List mo = this.motorRepository.findByProductId(id);
            List motors = new ArrayList();
            List mElecDataList = new ArrayList();
            Iterator var18 = mo.iterator();

            while(var18.hasNext()) {
               Motor motor = (Motor)var18.next();
               if (!"轴S".equals(motor.getAxisName())) {
                  motors.add(motor);
               } else {
                  mElecDataList.add(motor);
               }
            }

            product.setNcVer(ncVer);
            product.setServos(servos);
            product.setMotors(motors);
            product.setMServoVerList(mServoVerList);
            product.setMElecDataList(mElecDataList);
            productList.add(product);
         }

         List dataBeans = TransferProdcutAndDatabean.productsToDataBeans(productList);
         productResponseEntity.setMsgCode(0);
         productResponseEntity.setMsgDesc("查询成功");
         productResponseEntity.setCurrPage(productPage.getNumber());
         productResponseEntity.setTotalPage(productPage.getTotalPages());
         productResponseEntity.setTotalNum((new Long(productPage.getTotalElements())).intValue());
         productResponseEntity.setData(dataBeans);
      } catch (Exception var20) {
         Exception e = var20;
         productResponseEntity.setMsgCode(1);
         productResponseEntity.setMsgDesc(e.toString());
         WaLogger.logger.warn(e.toString());
      }

      return productResponseEntity;
   }

   @PostMapping({"/checkapi/updateProduct"})
   @ResponseBody
   public SimpleResponseEntity updateProduct(@RequestBody String json, HttpServletRequest request) {
      String ip = IpUtil.getIpAddr(request);
      SimpleResponseEntity simpleResponseEntity = new SimpleResponseEntity();

      try {
         ObjectMapper mapper = new ObjectMapper();
         DataBean dataBean = (DataBean)mapper.readValue(json, new TypeReference() {
         });
         Product product = TransferProdcutAndDatabean.dataBeanToProduct(dataBean);
         Product pro = (Product)this.productRepository.findById(product.getId()).get();
         User user = this.userService.findFirstByUsername(dataBean.getLoginName());
         if (user == null) {
            throw new Exception("用户不存在");
         }

         this.productService.update(product, user, ip);
         simpleResponseEntity.setMsgCode(0);
         simpleResponseEntity.setMsgDesc("修改成功");
      } catch (Exception var10) {
         Exception e = var10;
         simpleResponseEntity.setMsgCode(1);
         simpleResponseEntity.setMsgDesc(e.toString());
         WaLogger.logger.warn(e.toString());
      }

      return simpleResponseEntity;
   }

   @PostMapping({"/checkapi/delProduct"})
   @ResponseBody
   public SimpleResponseEntity delProduct(@RequestBody String json, HttpServletRequest request) {
      String ip = IpUtil.getIpAddr(request);
      SimpleResponseEntity simpleResponseEntity = new SimpleResponseEntity();

      try {
         ObjectMapper mapper = new ObjectMapper();
         DataBean dataBean = (DataBean)mapper.readValue(json, new TypeReference() {
         });
         Product product = TransferProdcutAndDatabean.dataBeanToProduct(dataBean);
         product = (Product)this.productRepository.findById(product.getId()).get();
         User user = this.userService.findFirstByUsername(dataBean.getLoginName());
         this.productService.delete(product, user, ip);
         simpleResponseEntity.setMsgCode(0);
         simpleResponseEntity.setMsgDesc("删除成功");
      } catch (Exception var9) {
         Exception e = var9;
         simpleResponseEntity.setMsgCode(1);
         simpleResponseEntity.setMsgDesc(e.toString());
         WaLogger.logger.warn(e.toString());
      }

      return simpleResponseEntity;
   }
}
